package com.alienlab.finder.web.rest;
import com.alienlab.finder.domain.TaskDetails;
import com.alienlab.finder.domain.Warning;
import com.alienlab.finder.service.TaskDetailsService;
import com.alienlab.finder.web.rest.errors.BadRequestAlertException;
import com.alienlab.finder.web.rest.util.HeaderUtil;
import com.alienlab.finder.web.rest.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing TaskDetails.
 */
@RestController
@RequestMapping("/api")
public class TaskDetailsResource {

    private final Logger log = LoggerFactory.getLogger(TaskDetailsResource.class);

    private static final String ENTITY_NAME = "taskDetails";

    private final TaskDetailsService taskDetailsService;

    public TaskDetailsResource(TaskDetailsService taskDetailsService) {
        this.taskDetailsService = taskDetailsService;
    }

    /**
     * POST  /task-details : Create a new taskDetails.
     *
     * @return the ResponseEntity with status 201 (Created) and with body the new taskDetails, or with status 400 (Bad Request) if the taskDetails has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiOperation(value = "增加监控任务详细信息")
    @PostMapping("/task-details")
    public ResponseEntity createTaskDetails(@RequestParam String task_id,@RequestParam String position_x,@RequestParam String position_y) throws Exception {
        log.debug("REST request to create TaskDetails ");
        if ("".equals(task_id) || task_id== null) {
            throw new BadRequestAlertException("未获取到task_id", ENTITY_NAME, "task_id do not exist");
        }
        if ("".equals(position_x) || position_x==null){
            throw new BadRequestAlertException("未获取到position_x", ENTITY_NAME, "position_x do not exist");

        }
        if ("".equals(position_y) || position_y==null){
            throw new BadRequestAlertException("未获取到position_y", ENTITY_NAME, "position_y do not exist");
        }
        Warning result = taskDetailsService.addPositionInfomation(Long.valueOf(task_id) , position_x, position_y);
        if (result!=null){
            return ResponseEntity.ok(result);

        }else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new Exception());
        }

    }

    @ApiOperation(value = "获取静态路径")
    @GetMapping("/task-details/staticmap")
    public ResponseEntity getStaticRouteMap(@RequestParam String task_id) throws Exception {
        log.debug("REST request to create TaskDetails ");
        if ("".equals(task_id) || task_id == null) {
            throw new BadRequestAlertException("未获取到task_id", ENTITY_NAME, "task_id do not exist");
        }
        List<TaskDetails> result = taskDetailsService.getRoutes(Long.valueOf(task_id));
        if (result!=null){
            return ResponseEntity.ok(result);

        }else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new Exception());
        }

    }

    /**
     * PUT  /task-details : Updates an existing taskDetails.
     *
     * @param taskDetails the taskDetails to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated taskDetails,
     * or with status 400 (Bad Request) if the taskDetails is not valid,
     * or with status 500 (Internal Server Error) if the taskDetails couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/task-details")
    public ResponseEntity<TaskDetails> updateTaskDetails(@Valid @RequestBody TaskDetails taskDetails) throws URISyntaxException {
        log.debug("REST request to update TaskDetails : {}", taskDetails);
        if (taskDetails.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        TaskDetails result = taskDetailsService.save(taskDetails);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, taskDetails.getId().toString()))
            .body(result);
    }

    /**
     * GET  /task-details : get all the taskDetails.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of taskDetails in body
     */
    @GetMapping("/task-details")
    public ResponseEntity<List<TaskDetails>> getAllTaskDetails(Pageable pageable) {
        log.debug("REST request to get a page of TaskDetails");
        Page<TaskDetails> page = taskDetailsService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/task-details");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
     * GET  /task-details/:id : get the "id" taskDetails.
     *
     * @param id the id of the taskDetails to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the taskDetails, or with status 404 (Not Found)
     */
    @GetMapping("/task-details/{id}")
    public ResponseEntity<TaskDetails> getTaskDetails(@PathVariable Long id) {
        log.debug("REST request to get TaskDetails : {}", id);
        Optional<TaskDetails> taskDetails = taskDetailsService.findOne(id);
        return ResponseUtil.wrapOrNotFound(taskDetails);
    }

    /**
     * DELETE  /task-details/:id : delete the "id" taskDetails.
     *
     * @param id the id of the taskDetails to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/task-details/{id}")
    public ResponseEntity<Void> deleteTaskDetails(@PathVariable Long id) {
        log.debug("REST request to delete TaskDetails : {}", id);
        taskDetailsService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
